<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Bulletin/classes/PhpFox_ServiceBulletin.class.php, $Author:  $ */



/** Service for working with messages functionality (all types)
 * 
 * @package module.messenger
 * @version $Id: 
 *   PHPFox Team ()
 */
class PhpFox_Mod_Bulletin_ServiceBulletin extends BaseService
{
    /** List users messages (inbox, sentbox)
     *
     * @param  array  $aCond     condition array
     * @param  int    $iPage	 page number
     * @param  int	  $iPageSize page size (row per page)
     * @param  string $sOrder	 order string
     * @return array  $aRes  	 Result
     */
    function &listBulletins($aCond = array(), $iPage=0, $iPageSize=0, $sOrder = '')
    {
        $oBulletin = &$this->_oModule->getItem('Bulletin');

        $aRes = $oBulletin->getList($aCond, $iPage, $iPageSize, $sOrder);
        return $aRes;
    }
    
    function checkStatus($iId, $sUser)
    {
        $oBulletin = &$this->_oModule->getItem('Bulletin');
        $sSql = 'SELECT COUNT(id) FROM '.App::getT('board_view').' WHERE view_id = '.$iId.' AND `user` = "'.$sUser.'"';
        
        return $oBulletin->oDb->getField($sSql) ? true : false;
    }
    
    function delItems($mAccIds)
    {
        if (!is_array($mAccIds))
        {
            $mAccIds = array($mAccIds);
        }
        $oBulletin = $this->_oModule->getItem('Bulletin');
        if ($mAccIds)
        {
            /** @TODO callback */  
            //$this->_oModule->callback('Shoutbox::delete', array('mAccIds' => $mAccIds));
            $oBulletin->delete($mAccIds);
            return true;
        }
    }
    
    function getBulletinById($iId, $sType = 'all')
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        switch ($sType)
        {
            case 'user':
                $mRes = $oBulletin->oDb->getField('SELECT `user` FROM '.App::getT('board').' WHERE id = '.$iId);
                break;
            default:
                $mRes = $oBulletin->oDb->getRow('SELECT b.id, b.user, b.title, b.text, b.time, u.id AS userid FROM '.App::getT('board').' AS b, '.App::getT('user').' AS u WHERE b.id = "'.$iId.'" AND b.user = u.user');
                break;
        }
        
        return $mRes;
    }
    
    function getNavigation($sCond, $iId)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        $iPrev = $oBulletin->oDb->getField('SELECT `id` FROM '.$oBulletin->sTable.' WHERE id < '.$iId.$sCond.' ORDER BY `id` DESC LIMIT 1');
        $iNext = $oBulletin->oDb->getField('SELECT `id` FROM '.$oBulletin->sTable.' WHERE id > '.$iId.$sCond.' ORDER BY `id` ASC LIMIT 1');
        return array($iPrev, $iNext);
    }
    
    function isNewBoard($iId, $sUser)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        return $oBulletin->oDb->getField('SELECT `id` FROM '.App::getT('board_view').' WHERE view_id = '.$iId.' AND `user` = "'.$sUser.'"') ? false : true;
    }
    
    function setStatusView($iId, $sUser)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        $oBulletin->aData['user']    = $sUser;
        $oBulletin->aData['view_id'] = $iId;
        $oBulletin->oDb->insert(App::getT('board_view'), $oBulletin->aData);        
    }
    
    function addBulletin($aData, $sUser)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        
        $oBulletin->aData['user']   = '"'.$sUser.'"';
        $oBulletin->aData['title']  = '"'.mysql_escape_string(htmlspecialchars($aData['title'])).'"';
        $oBulletin->aData['text']   = '"'.mysql_escape_string(str_replace(array('&lt;', '&gt;', '&quot;', '&amp;'), array('<', '>', '"', '&'), $aData['text'])).'"';
        $oBulletin->aData['time']   =  time();
        
        $oBulletin->insert(false);
    }
    
    function updateBulletin($iId, $aData)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        $oBulletin->aData['id']     = $iId;
        $oBulletin->aData['title']  = '"'.mysql_escape_string(htmlspecialchars($aData['title'])).'"';
        $oBulletin->aData['text']   = '"'.mysql_escape_string(str_replace(array('&lt;', '&gt;', '&quot;', '&amp;'), array('<', '>', '"', '&'), $aData['text'])).'"';
        
        $oBulletin->update(array(), false);
    }
    
    function checkLocation($sUser)
    {
        $oBulletin = $this->_oModule->getItem('Bulletin');
        return $oBulletin->oDb->getField('SELECT location FROM '.App::getT('user').' WHERE `user` = "'.$sUser.'"');
    }
}
?>